package top.dingwen.io.treasure.es.autoconfigure;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.starter.register.EsMapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import top.dingwen.io.treasure.es.constant.EsConstant;
import top.dingwen.io.treasure.es.web.EsIndexController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * es场景自动配置类
 *
 * @author dingwen
 * @since 2024/10/16 13:28
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(EsProperties.class)
@Lazy(value = false)
@Slf4j
@EsMapperScan("top.dingwen.io.treasure.es.mapper")
public class EsAutoconfiguration {

    @Resource
    private EsProperties esProperties;

    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        if (log.isInfoEnabled()) {
            log.info("{}\n\n " +
                            "            \n" +
                            "            \n" +
                            "   ___  ___ \n" +
                            "  / _ \\/ __|\n" +
                            " |  __/\\__ \\\n" +
                            "  \\___||___/\n" +
                            "            \n" +
                            "            \n"
                    , EsConstant.L_P_S);
            log.info("{},读取到的配置如下:\n{}", EsConstant.L_P_S, JSONUtil.toJsonPrettyStr(esProperties));
            log.info("{},API参考官网，地址:{}", EsConstant.L_P_S, "https://www.easy-es.cn");
            log.info("{}，启动成功", EsConstant.L_P_S);
        }
    }

    /**
     * 默认API
     *
     * @return {@link EsIndexController }
     */
    @ConditionalOnProperty(name = "dingwen.treasure.es.defaultApiEnabled", havingValue = "true")
    @Bean
    public EsIndexController esIndexController() {
        log.info("{},{},已成功加载", EsConstant.L_P_S, EsConstant.L_P_API);
        return new EsIndexController();
    }
}
